package com.bawei.module_repair.viewmodel

import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.module_repair.model.RepairAddBody
import com.bawei.module_repair.model.RepairModelItem
import com.bawei.module_repair.repositort.RepairRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import javax.inject.Inject

@HiltViewModel
class RepairViewModel @Inject constructor(
    private val mRepository: RepairRepository
) : BaseViewModel() {
    //维修列表
    private val _repairEvent = MutableSharedFlow<RequestEvent>()
    val repairEvent get() = _repairEvent.asSharedFlow()
    var repairList: MutableList<RepairModelItem> = mutableListOf()
    //报修
    private val _repairAddEvent = MutableSharedFlow<RequestEvent>()
    val repairAddEvent get() = _repairAddEvent.asSharedFlow()
    //删除报修
    private val _repairDeleteEvent = MutableSharedFlow<RequestEvent>()
    val repairDeleteEvent get() = _repairDeleteEvent.asSharedFlow()
    fun getsRepairList(){
        execute(
            mRepository.getRepairApply(),
            onStart = {
                _repairEvent.emit(Loading)
            },
            onError = {
                _repairEvent.emit(Failed(it))
            },
            onFinish = {
                repairList.clear()
                repairList.addAll(it.data)
                _repairEvent.emit(Success)
            }
        )
    }
    fun addRepair(repairAddBody: RepairAddBody){
        execute(
            mRepository.getRepairApplyAdd(repairAddBody),
            onStart = {
                _repairAddEvent.emit(Success)
            },
            onError = {
                _repairAddEvent.emit(Failed(it))
            },
            onFinish = {
                _repairAddEvent.emit(Success)
            }
        )
    }
    fun deleteRepair(id: Int){
        execute(
            mRepository.getRepairApplyDelete(id),
            onStart = {
                _repairDeleteEvent.emit(Success)
            },
            onError = {
                _repairDeleteEvent.emit(Failed(it))
            },
            onFinish = {
                _repairDeleteEvent.emit(Success)
            }
        )
    }
}